package com.all.learning.guava.io;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.junit.Test;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;

public class FilesTest1 {
	File f = new File("D://var");

	//@Test
	//public void test1() {
	//	FluentIterable<File> fi = Files.fileTreeTraverser().breadthFirstTraversal(new File("C:/统计/log"));
	//	for (File file : fi) {
	//		System.out.println(file.getAbsolutePath());
	//	}
	//}
	//
	//@Test
	//public void testChildren() {
	//	Iterable<File> file = Files.fileTreeTraverser().children(new File("C:/统计/log"));
	//	for (File file2 : file) {
	//		System.out.println(file2.getAbsolutePath());
	//	}
	//}
	//
	//@Test
	//public void test3() {
	//	FluentIterable<File> fi = Files.fileTreeTraverser().preOrderTraversal(f);
	//	for (File file : fi) {
	//		System.out.println(file.getAbsolutePath());
	//	}
	//}

	@Test
	public void tes4() throws IOException {
		List<String> list = Files.readLines(f, Charsets.UTF_8, new MyLineRead());

	}

	private class MyLineRead implements LineProcessor<List<String>> {
		List<String> result = Lists.newArrayList();

		@Override
		public boolean processLine(String line) throws IOException {
			result.add(line.trim());
			if (line.contains("haha")) {
				return false;
			}else {
				return true;
			}
		}

		@Override
		public List<String> getResult() {
			return result;
		}

	}

	public static void main(String[] args) throws Exception {

		File from = new File("c://22.txt");
		File to = new File("c://22temp.txt");
		Files.copy(from, to);
		// Files.move(from, to);

		// 将文件读取为字符串列表
		// List<String> readLines = Files.readLines(from, Charsets.UTF_8);
//		File file = new File(Resources.getResource("time.txt").getFile());
		// 为文件生成hash值File from = new File("c://22.txt");
		HashCode code = Files.hash(from, Hashing.md5());
		// System.out.println(code.toString());

		// 写或追加文件
		// Files.write("hello world !".getBytes(), to);
		// Files.append("appendes string", to, Charset.defaultCharset());
		
		// ByteSource代表一个可读的字节源
		ByteSource byteSource = Files.asByteSource(from);
		byte[] array = byteSource.read();
		System.out.println(array);
//		Files.newReader(f, Charsets.UTF_8);
//		Files.newWriter(from, Charsets.UTF_8)
		
		// ByteSink代表一个可写的字节源
		// ByteSink byteSink = Files.asByteSink(to, FileWriteMode.APPEND);
		// byteSink.write(Files.toByteArray(from));

		// 编码
		// BaseEncoding encoding = BaseEncoding.base64();
		// String cc = encoding.encode(Files.toByteArray(to));
		// System.out.println(cc);
		//
		// byte[] array1 = encoding.decode(cc);
		// System.out.println(new String(array1));
	}

}
